home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / FLNORM.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  5KB  |  153 lines

  1. /*
  2. **  FLNORM.C - Normalize DOS file names
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #ifdef __TURBOC__
  15.  #include <dir.h>
  16. #else
  17.  #include <direct.h>
  18. #endif
  19. #include <dos.h>
  20. #include <io.h>
  21.  
  22. #define MAX_FLEN 67
  23. #define LAST_CHAR(string) (((char *)string)[strlen(string)-1])
  24.  
  25. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  26.  
  27. char *unix2dos(char *path);
  28.  
  29. int flnorm(char *in_name, char *out_name)
  30. {
  31.       LOGICAL dir_flag = FALSE, new_drv = FALSE, root_flag;
  32.       int status = 0, level = 0;
  33.       char *p, *out;
  34.       static char drive[2][3];
  35.       static char file[14];
  36.       static char I_am_here[MAX_FLEN];
  37.       static char I_am_there[MAX_FLEN];
  38.       static char remember[MAX_FLEN];
  39.  
  40.       getcwd(I_am_here, MAX_FLEN);
  41.       if (!in_name || !in_name[0])
  42.       {
  43.             strcpy(out_name, I_am_here);
  44.             goto ERRexit;
  45.       }
  46.       strncpy(drive[0], I_am_here, 2);
  47.       drive[0][2] = '\0';
  48.       if (':' == in_name[1])
  49.       {     /* If a drive is specified                            */
  50.             if (chdrv(in_name[0]))
  51.             {     /* If the drive is invalid                      */
  52.                   status = ERROR;
  53.                   goto ERRexit;
  54.             }
  55.             new_drv = TRUE;
  56.             getcwd(remember, MAX_FLEN);
  57.             strncpy(drive[1], remember, 2);
  58.             drive[1][2] = '\0';
  59.       }
  60.       else
  61.       {     /* If a drive isn't specified                         */
  62.             if (NULL != (p = strchr(in_name, ':')))
  63.             {     /* If filename is illegal                       */
  64.                   status = ERROR;
  65.                   goto ERRexit;
  66.             }
  67.       }
  68.       unix2dos(in_name);
  69.       if (new_drv)
  70.       {
  71.             if ('\\' == in_name[2])
  72.                   strcpy(out_name, drive[1]);
  73.             else
  74.             {
  75.                   strcpy(out_name, remember);
  76.                   if ('\\' != LAST_CHAR(remember))
  77.                         strcat(out_name, "\\");
  78.             }
  79.       }
  80.       else
  81.       {
  82.             strcpy(out_name, drive[0]);
  83.             if ('\\' != *in_name)
  84.             {
  85.                   strcat(out_name, I_am_here);
  86.                   if ('\\' != LAST_CHAR(I_am_here))
  87.                         strcat(out_name, "\\");
  88.             }
  89.       }
  90.       strcat(out_name, &in_name[(new_drv) ? 2 : 0]);
  91.       fln_fix(out_name);
  92.       out = &out_name[2];
  93.       if (!(*out))
  94.             goto ERRexit;
  95.       while ('\\' == LAST_CHAR(out))
  96.       {     /* Strip trailing `\'s                                */
  97.             LAST_CHAR(out_name) = '\0';
  98.             dir_flag = TRUE;
  99.       }
  100.       if (!(*out))
  101.       {
  102.             if (dir_flag)
  103.             {
  104.                   strcat(out, "\\");
  105.                   goto ERRexit;
  106.             }
  107.             else  goto BADPATH;
  108.       }
  109.       if (NULL != (p = strrchr(out_name, '\\')))
  110.             strcpy(file, p);        /* Save filename              */
  111.       if (chdir(out))
  112.       {     /* If can't move to path                              */
  113.             if ((!dir_flag) && p)
  114.             {     /* If there was a separate path                 */
  115.                   *p = '\0';
  116.                   if (!(*out))
  117.                   {     /* Back at the root, handle it            */
  118.                         strcpy(p, "\\");
  119.                         strcpy(file, &file[1]);
  120.                   }
  121.                   if (chdir(out))
  122.                   {     /* If we can't move to path               */
  123.                         *p = '\\';
  124.                         goto BADPATH;
  125.                   }
  126.                   ++level;          /* Flag we stripped name      */
  127.             }
  128.             else
  129.             {     /* No path as specified                         */
  130.                   if (p)
  131.                   {
  132. BADPATH:                status = ERROR;
  133.                         goto ERRexit;
  134.                   }
  135.             }
  136.       }
  137.       getcwd(I_am_there, MAX_FLEN); /* Get normalized path        */
  138.       strupr(I_am_there);
  139.       strcpy(out_name, I_am_there);
  140.       if (level)
  141.             strcat(out_name, file);
  142. ERRexit:
  143.       if (new_drv)
  144.       {
  145.             chdir(remember);
  146.             chdrv(I_am_here[0]);
  147.       }
  148.       chdir(I_am_here);
  149.       if (status)
  150.             out_name[0] = '\0';
  151.       return status;
  152. }
  153.